<template>
  <div class="login-container">
    <van-nav-bar title="登录" />
    <!-- 登录表单 -->
    <van-form ref="loginForm" @submit="onSubmit">
      <van-field
        v-model="userData.mobile"
        name="mobile"
        type="number"
        maxlength="11"
        placeholder="请输入手机号"
        :rules="formRule.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>

      <van-field
        v-model="userData.code"
        type="number"
        name="code"
        placeholder="请输入验证码"
        @field-error-message-font-size="8"
        :rules="formRule.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <!-- 倒计时器 -->
          <van-count-down
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
            :time="10 * 1000"
            format="ss 秒"
          />
          <!-- 发送验证码按钮 -->
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            native-type="button"
            @click="sendMsg"
          >
            发送验证码
          </van-button>
        </template>
      </van-field>

      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info">登录</van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
import { login, getSmsCode } from '@/api/user.js'

export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      // 登录用户表单数据
      userData: {
        mobile: '18620505272',
        code: '246810'
      },
      // 表单规则对象
      formRule: {
        mobile: [
          { required: true, message: '必填哦~亲~' },
          {
            validator(val) {
              return val.length === 11
            },
            message: '长度必须11位'
          }
        ],
        code: [
          { required: true, message: '必填哦~亲~' },
          {
            validator(val) {
              return val.length === 6
            },
            message: '长度必须是6个字符'
          }
        ]
      },
      // 是否显示倒计时
      isCountDownShow: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 1.登录表单的 提交事件------------------------------------
    async onSubmit() {
      this.$toast.loading({
        message: '登录中...',
        duration: 0
      })
      // a.获取 用户表单数据
      const data = this.userData
      // b.校验表单
      // c.提交表单数据到 服务器接口
      try {
        const { data: res } = await login(data.mobile, data.code)
        console.log(res)
        // 将 token 保存到 vuex
        this.$store.commit('setUserToken', res.data)
        this.$toast.success('登录成功~')
        this.$router.push('/my')
      } catch (err) {
        console.log(err)
        this.$toast.success('登录失败~')
      }
    },
    // 2.发送验证码--------------------------------------------
    async sendMsg() {
      // 显示倒计时
      this.isCountDownShow = true
      // a.校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (error) {
        // 如果 校验失败，则 停止方法执行
        return console.log('校验失败，页面已经有显示！')
      }

      // b.发送验证码的 业务代码--------------------------------
      try {
        await getSmsCode(this.userData.mobile)
        this.$toast.success('获取成功哦~')
      } catch (err) {
        this.$toast.fail('获取失败哦~亲')
        // todo: 判断 响应状态码 === 429，如果是，则发送太频繁
        // err.reponse.status
        console.log(err)
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  border: 1px solid #000;
  /deep/ ul > li {
    border: 1px solid #000;
  }
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 160px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 20px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
